package com.tanhua.dubbo.api;

import com.tanhua.domain.db.User;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.FocusUser;
import com.tanhua.domain.mongo.Movement;
import com.tanhua.domain.mongo.Video;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.dubbo.utils.IdService;
import com.tanhua.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.Date;
import java.util.List;

@Service
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public Integer saveComment(Comment comment) {

        ObjectId publishId = comment.getPublishId(); // 有可能是圈子的ID、小视频的id、评论的id
        Integer commentType = comment.getCommentType();// 1 2 3

        if(commentType==1||commentType==2||commentType==3){  //圈子  点赞  评论 喜欢


//        1、对圈子点赞数+1
//            update Movement set likecount=likecount+1 where id=?
            Query query = new Query(Criteria.where("id").is(publishId));
            Update update = new Update();
            update.inc(comment.getCol(),1);
            mongoTemplate.updateFirst(query,update,Movement.class);


//            movement = mongoTemplate.findById(publishId, Movement.class);

//          2、保存点赞数据
            Movement movement = mongoTemplate.findById(publishId, Movement.class);
//        补充comment中属性
            comment.setId(new ObjectId());
            comment.setCreated(new Date().getTime());
            comment.setPublishUserId(movement.getUserId()); //动态的发布人

            mongoTemplate.save(comment);

 //        3、返回这个圈子的点赞数
            return comment.getCount(movement); // 有可能返回是点赞数、评论数、喜欢数


        }else if(commentType==4||commentType==5){  //小视频

            //评论
            mongoTemplate.findById(publishId, Comment.class);
            comment.setId(new ObjectId());
            comment.setCreated(new Date().getTime());
            comment.setPublishUserId(comment.getUserId());
            mongoTemplate.save(comment);



        }else if(commentType==6){   //评论点赞
//            对评论的点赞数加1


        }

        return null;
    }
// 先创建 一会儿补充代码


    @Override
    public Integer deleteComment(Comment comment) {
        ObjectId publishId = comment.getPublishId(); // 有可能是圈子的ID、小视频的id、评论的id

        Integer commentType = comment.getCommentType();// 1 2 3

        if(commentType==1||commentType==2||commentType==3){  //点赞  评论 喜欢


//        1、对圈子点赞数-1
//            update Movement set likecount=likecount+1 where id=?
            Query query = new Query(Criteria.where("id").is(publishId));
            Update update = new Update();
            update.inc(comment.getCol(),-1);
            mongoTemplate.updateFirst(query,update,Movement.class);


//            movement = mongoTemplate.findById(publishId, Movement.class);

//          2、删除点赞数据

//
            Query query1 = new Query(Criteria.where("publishId").is(publishId).and("userId").is(comment.getUserId()).and("commentType").is(comment.getCommentType()));
            mongoTemplate.remove(query1,Comment.class);


            //            delete  from comment where publishId=?? and userId=?? and commentType=1
            Movement movement = mongoTemplate.findById(publishId, Movement.class);
            //        3、返回这个圈子的点赞数
            return comment.getCount(movement); // 有可能返回是点赞数、评论数、喜欢数
        }else if(commentType==4||commentType==5){  //小视频


        }else if(commentType==6){   //评论点赞


        }

        return null;
    }

    @Override
    public PageResult queryComments(String movementId, Integer page, Integer pagesize) {
//        分页查询指定动态的评论数据 select * from comment where  publishId=?? and  commentType=2
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(2));
        query.with(Sort.by(Sort.Direction.DESC,"created"));
        query.limit(pagesize).skip((page-1)*pagesize);

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        return new PageResult(page,pagesize,(int)count,commentList);
    }

    @Override
    public PageResult queryCommentsPeople(Long userId, Integer page, Integer pagesize, int commentType) {
//        db.comment.find({commentType:3,publishUserId:1})
        Query query = new Query(Criteria.where("publishUserId").is(userId).and("commentType").is(commentType));
        query.with(Sort.by(Sort.Direction.DESC,"created"));
        query.limit(pagesize).skip((page-1)*pagesize);
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        return new PageResult(page,pagesize,(int)count,commentList);
    }

}